﻿using AutoMapper;
using MediatR;
using RearLivestock.Common.Domains.hhx;
using RearLivestock.Common.Domains.hq;
using RearLivestock.Common.Dtos;
using RearLivestock.Common.Infrastructure.Interface;
using RearLivestock.RBAC.ErrorCodeEnum;

namespace RearLivestock.RBAC.Api.Application.hqApplication.Commands.Ablactationss
{
    public class UpdateAblactationHander : IRequestHandler<UpdateAblactationCommand, Result>
    {
        private readonly IMapper mapper;

        private readonly IBaseRepository<Ablactations> ablactationsRepository;

        public UpdateAblactationHander(IMapper mapper, IBaseRepository<Ablactations> ablactationsRepository)
        {
            this.mapper = mapper;
            this.ablactationsRepository = ablactationsRepository;
        }

        public async Task<Result> Handle(UpdateAblactationCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Updated };
            try
            {
                Ablactations Ablactations = mapper.Map<Ablactations>(request);
                var res = await ablactationsRepository.Update(Ablactations);
                result.Code = (int)CommonErrorCode.Success;
                result.Msg = "母猪断奶修改成功";
            }
            catch (Exception)
            {
                result.Msg = "母猪断奶信息修改异常";
            }
            return result;
        }
    }
}
